﻿/*
 * Copyright(C) 2012,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：EditableModelBase.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2012年6月12日星期二
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ajayumi.Platform.Core.Model
{
    /// <summary>
    /// 可编辑的实体类基类
    /// </summary>
    public abstract class EditableModelBase : ModelBase, IEditableObject
    {
        private bool m_IsEditing = false;            //是否处于编辑状态
        private HistoryCollection m_History = null;

        protected HistoryCollection History
        {
            get { return this.m_History; }
        }

        internal EditableModelBase()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this.m_History = new HistoryCollection();
        }

        /// <summary>
        /// 备份修改前的值
        /// </summary>
        protected abstract void BackupRecord();

        /// <summary>
        /// 还原修改前的值
        /// </summary>
        protected abstract void RestoreRecord();

        /// <summary>
        /// 取消编辑
        /// </summary>
        protected virtual void InvokeCancelEdit()
        {
            this.RestoreRecord();
        }

        #region IEditableObject 成员

        public void BeginEdit()
        {
            // TODO:  添加 EditableModel.BeginEdit 实现
            Console.WriteLine("Start BeginEdit");
            if (!m_IsEditing) 
            {
                m_IsEditing = true;
                this.BackupRecord();
            }
            Console.WriteLine("End BeginEdit");
        }

        public void CancelEdit()
        {
            // TODO:  添加 EditableModel.CancelEdit 实现
            Console.WriteLine("Start CancelEdit");
            if (m_IsEditing) 
            {
                m_IsEditing = false;
                this.InvokeCancelEdit();
            }
            Console.WriteLine("End CancelEdit");
        }

        public void EndEdit()
        {
            // TODO:  添加 EditableModel.EndEdit 实现
            Console.WriteLine("Start EndEdit");
            if (m_IsEditing)
            {
                m_IsEditing = false;
            }
            Console.WriteLine("End EndEdit");
        }

        #endregion
    }
}
